package com.instalinks.android.smspostservice.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.instalinks.android.smspostservice.AppTabBaseActivity;
import com.instalinks.android.smspostservice.R;
import com.instalinks.android.smspostservice.utils.Constants;
import com.instalinks.android.smspostservice.utils.enums.TimeOuts;

public class AdminConfigActivity extends AppTabBaseActivity {
	private TextView txtDeviceID;
	private TextView txtSubscriberID;
	private TextView txtURL;
	private TextView txtURLDestination;

	private TextView txtSCODE;

	private TextView txtBattery;
	private TextView txtSignal;
	private TextView txtLocation;
	private TextView txtGprs;
	private TextView txtVersion;
	private TextView txtFailMsg;
	private TextView txtCounters;
	private TextView txtFreeCount;
	private TextView txtPaid;

	private Handler configReloadHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initialize();
	}

	private void initialize() {
		mAppCfg.rePopulate();

		this.setContentView(R.layout.configuration);

		txtDeviceID = new TextView(getApplicationContext());
		txtSubscriberID = new TextView(getApplicationContext());
		txtSCODE = new TextView(getApplicationContext());

		txtURL = new TextView(getApplicationContext());
		txtURLDestination = new TextView(getApplicationContext());
		txtBattery = new TextView(getApplicationContext());
		txtSignal = new TextView(getApplicationContext());
		txtLocation = new TextView(getApplicationContext());
		txtGprs = new TextView(getApplicationContext());
		txtCounters = new TextView(getApplicationContext());
		txtVersion = new TextView(getApplicationContext());
		txtFailMsg = new TextView(getApplicationContext());

		txtFreeCount = new TextView(getApplicationContext());
		txtPaid = new TextView(getApplicationContext());

		TableLayout tLayout = (TableLayout) this.findViewById(R.id.tblConfig);

		tLayout.addView(addRow("DeviceID : ", txtDeviceID));
		tLayout.addView(addRow("SubID : ", txtSubscriberID));
		tLayout.addView(addRow("URL : ", txtURL));
		tLayout.addView(addRow("URL Dest. : ", txtURLDestination));
		tLayout.addView(addRow("SCODE : ", txtSCODE));
		tLayout.addView(addRow("Battery : ", txtBattery));
		tLayout.addView(addRow("Signal : ", txtSignal));
		tLayout.addView(addRow("Location : ", txtLocation));
		tLayout.addView(addRow("Gprs : ", txtGprs));
		tLayout.addView(addRow("Counters : ", txtCounters));
		tLayout.addView(addRow("Version : ", txtVersion));
		tLayout.addView(addRow("FailMsg : ", txtFailMsg));
		tLayout.addView(addRow("Free Count: ", txtFreeCount));
		tLayout.addView(addRow("Paid :", txtPaid));
		loadConfigSettings();

		configReloadHandler = new Handler();
		configReloadHandler.removeCallbacks(configReloader);
		configReloadHandler.post(configReloader);
	}

	private TableRow addRow(String title, TextView txtValue) {
		TableRow tr = new TableRow(getApplicationContext());

		TextView txtTitle = new TextView(getApplicationContext());
		txtTitle.setText(title);

//		txtTitle.setWidth(80);
		txtTitle.setTextSize(16);
		txtTitle.setTextColor(Color.BLACK);

		txtValue.setTextColor(Color.BLACK);
		txtValue.setTextSize(16);

		tr.addView(txtTitle);
		tr.addView(txtValue);

		return tr;
	}

	private void loadConfigSettings() {
		mAppCfg.rePopulate();

		txtDeviceID.setText(mAppCfg.getDeviceId());
		txtSubscriberID.setText(mAppCfg.getSubscriberId());
		txtURL.setText(Constants.APP_CONFIG_URL);
		txtURLDestination.setText(mAppCfg.getDataPostUrl());

		txtSCODE.setText(mAppCfg.getScode());
		
		txtBattery.setText(String.valueOf(mAppCfg.isBatteryFlag()));
		txtSignal.setText(String.valueOf(mAppCfg.isSignalFlag()));
		txtLocation.setText(String.valueOf(mAppCfg.isLocationFlag()));
		txtGprs.setText(String.valueOf(mAppCfg.isGprsFlag()));
		txtCounters.setText(String.valueOf(mAppCfg.isCountersFlag()));
		
		txtVersion.setText(mAppCfg.getVersion());
		txtFailMsg.setText(mAppCfg.getFailReplyMsg());
		txtFreeCount.setText(String.valueOf(mAppCfg.getFreeCount()));
		txtPaid.setText(String.valueOf(mAppCfg.isPaidFlag()));
	}

	private Runnable configReloader = new Runnable() {
		@Override
		public void run() {
			loadConfigSettings();
			configReloadHandler.postDelayed(this, TimeOuts.TenSec.getValue());
		}
	};

	@Override
	protected boolean doBackgroundWork() {
		return true;
	}

	@Override
	protected void doOnSuccess() {
	}

	@Override
	protected void doOnFailure() {
	}
}
